package net.sashag.nadlandroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class ApartmentOverlay extends ItemizedOverlay<OverlayItem> {

	private List<OverlayItem> items = new ArrayList<OverlayItem>();
	private List<Apartment> apartments = new ArrayList<Apartment>();
	private Context context;

	public void addApartment(Apartment apartment) {
		items.add(new OverlayItem(new GeoPoint(
				(int) (apartment.getLatitude() * 1E6),
				(int) (apartment.getLongitude() * 1E6)),
				Integer.toString(apartment.getPrice()),
				String.format("Address: %s\nPrice: %d\nBedrooms: %d",
						apartment.getAddress(), apartment.getPrice(), apartment.getBedrooms())));
		apartments.add(apartment);
		populate();
	}

	public void clearApartments() {
		items.clear();
		apartments.clear();
		populate();
	}

	public ApartmentOverlay(Context ctx, Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		context = ctx;
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return items.get(i);
	}

	@Override
	public int size() {
		return items.size();
	}

	@Override
	protected boolean onTap(int index) {
		Apartment apartment = apartments.get(index);
		new SmallApartmentDetailsActivity_.IntentBuilder_(context).apartment(apartment).start();
		return super.onTap(index);
	}

	public List<Apartment> getApartments() {
		return apartments;
	}

}
